// pages/operation-center/add-contract/index.js
const app = getApp()
Page({
  data: {
    projectInfo: {},
    deviceList: [],
    deviceDict: [],
  },
  onLoad(option) {
    if (option.id) {
      this.getDeviceDict(option.id)
    }
  },
  getDeviceDict(id) {
    app.axios('device', {
      op: 'ajaxGbdcAll'
    }).then(res => {
      if (res.code) {
        this.setData({
          deviceDict: res.lists
        })
        this.getProjectInfo(id)
      }
    })
  },
  getProjectInfo(id) {
    app.axios('item', {
      op: 'ajaxItemDetail',
      id: id
    }).then(res => {
      if (res.code) {
        this.setData({
          projectInfo: res.item
        })
        if (res.item.dids.length > 0) {
          this.getDeviceList(res.item.dids)
        }
      }
    })
  },
  getDeviceList(ids) {
    const idSet = new Set(ids.map(id => String(id)));
    const filteredDevices = this.data.deviceDict.filter(device =>
      idSet.has(String(device.id))
    );
    this.setData({
      deviceList: filteredDevices
    })
  }
})